package color.koitq.picker.activty

import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import color.koitq.picker.R
import color.koitq.picker.ad.AdActivity
import color.koitq.picker.view.CustomFlag
import com.blankj.utilcode.util.ClipboardUtils
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.ResourceUtils
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import com.skydoves.multicolorpicker.ColorEnvelope
import com.skydoves.multicolorpicker.listeners.ColorListener
import kotlinx.android.synthetic.main.fragment_image_color.*

class ImageColorActivity : AdActivity() {

    override fun getContentViewId() = R.layout.fragment_image_color


    override fun init() {
        topbar.setTitle("取色")
        topbar.addLeftBackImageButton().setOnClickListener { finish() }
        var path = intent.getStringExtra("path")
        if (path.isNullOrEmpty()) {
            return
        }
        Glide.with(this).asBitmap().load(path).into(object : CustomTarget<Bitmap>() {
            override fun onLoadCleared(placeholder: Drawable?) = Unit
            override fun onResourceReady(bitmap: Bitmap, transition: Transition<in Bitmap>?) {
                hideLoading()
                multiColorPickerView.setPaletteDrawable(ConvertUtils.bitmap2Drawable(bitmap))
            }
        })
        multiColorPickerView.addSelector(
            ResourceUtils.getDrawable(R.mipmap.wheel),
            object : ColorListener {
                @SuppressLint("SetTextI18n")
                override fun onColorSelected(envelope: ColorEnvelope) {
                    if (envelope.color == 0) return
                    tv_hex.text = "#${envelope.htmlCode}"
                    val rgb = envelope.rgb
                    tv_rgb.text=""
                    var s = StringBuffer()
                    for (i in rgb) {
                        s.append("${i},")
                    }
                    tv_rgb.text=s.substring(0,s.length-1)

                }
            })
        multiColorPickerView.setFlagView(CustomFlag(activity, R.layout.layout_flag))

        multiColorPickerView.setSelectedAlpha(0.6f)

        view1.setOnClickListener {
            ClipboardUtils.copyText(tv_hex.text.toString())
            ToastUtils.showLong("已复制到剪贴板")
        }
        view2.setOnClickListener {
            ClipboardUtils.copyText(tv_rgb.text.toString())
            ToastUtils.showLong("已复制到剪贴板")
        }
        view3.setOnClickListener {
            ClipboardUtils.copyText(tv_hex.text.toString())
            ToastUtils.showLong("已复制到剪贴板")
        }
        showSecondPageAd_TwoBanner(bannerView,bannerView2)
    }


}